package com.rent.common.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @Author zhang
 * @Create 2022/7/5 9:31
 */
@Configuration
public class BigDecimalJsonSerializer extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (bigDecimal != null) {
            // 处理当数值为 0.00000001类似的小数时，自动转为科学计数法的问题
            jsonGenerator.writeString(bigDecimal.toPlainString());
        }
    }
}
